
class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& temperatures) {
        int n = temperatures.size();
        vector<int>res(n);
        stack<int>a;
        for (int i = 0; i < n; i++) {
            while (!empty(a) && temperatures[i] > temperatures[a.top()]) {
                res[a.top()] = i - a.top();
                a.pop();
            }
            a.push(i);
        }
        return res;
    }
};